package com.put.mybills.activities;

import java.util.List;
import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.model.Bill;
import com.put.mybills.model.Unit;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class BillActivity extends Activity {
	
	
	private TextView currentBillName;
	private TextView currentBillDescription;
	private TextView currentBillPrice;
	private TextView currentBillDate;	
	private TableLayout currentBillUnits;
	private Bitmap currentTakePhoto;
	private TextView currentBillUnitTitle;
	private TextView currentBillPictureTitle;
	private TextView current_bill_paid;
	private ImageView currentPicture;
	
	private Bill bill;
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {				
		getMenuInflater().inflate(R.menu.main_bill_activity, menu);    	
        return true;
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		
	    switch (menuItem.getItemId()) {
	    case android.R.id.home:	      
	    	super.onBackPressed();
	      	break;
	    case R.id.remove_current_bill:
	    	removeBill();
	    	super.onBackPressed();
	    	break;
	    }
	    
	  return (super.onOptionsItemSelected(menuItem));
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
	    getActionBar().setHomeButtonEnabled(true);
	    getActionBar().setTitle(R.string.back);
	    
	    currentBillName = (TextView) findViewById(R.id.current_bill_name);
	    currentBillDescription = (TextView) findViewById(R.id.current_bill_description);
	    currentBillPrice  = (TextView) findViewById(R.id.current_bill_price);
	    currentBillDate  = (TextView) findViewById(R.id.current_bill_date);
	    currentBillUnits = (TableLayout) findViewById(R.id.table_units_result);
	    currentBillUnitTitle = (TextView) findViewById(R.id.current_bill_unit_title);
	    currentBillPictureTitle = (TextView) findViewById(R.id.current_bill_picture_title);
	    current_bill_paid  = (TextView) findViewById(R.id.current_bill_paid);
	    
	    currentPicture = (ImageView) findViewById(R.id.current_bill_picture);
	    
	    
	    
	    if(getIntent().getStringExtra("BillName") != null){
	    	bill = GlobalParams.getInstance().billdatasource.getBill(getIntent().getStringExtra("BillName"));
			
		   if (bill.getName() != null)
		    	currentBillName.setText(bill.getName());
		    
		   if (!bill.getDescription().equals(""))		    
		    	 currentBillDescription.setText(bill.getDescription());
		   else {
			   currentBillDescription.setVisibility(TextView.GONE);
			   currentBillDescription.invalidate();
		   }
		   if (bill.getIfPaid() == 0) 
			   current_bill_paid.setText(getResources().getString(R.string.bill_notpaid));
		   else
			   current_bill_paid.setText(getResources().getString(R.string.bill_paid));
		   
		    	
		   if (bill.getPrice() != null)
		    	currentBillPrice.setText(bill.getPrice().toString());
		    
		   if (bill.getDate() != null)
		    	 currentBillDate.setText(bill.getDate());
		  
		   List<Unit> unit = GlobalParams.getInstance().unitdatasource.getAllUnitForBill(bill.getId());	  	    	 
		   if (unit.size()>0){
			  createTableUnitResult(bill.getId());
		   }
		   else {
			   currentBillUnitTitle.setVisibility(TextView.GONE);
			   currentBillUnitTitle.invalidate();
			   
			   currentBillUnits.setVisibility(TableLayout.GONE);
			   currentBillUnits.invalidate();
			   
		   }
		   
		   if (bill.getPicturePath() != null && !bill.getPicturePath().equals(""))
		   {
			   
			   Log.d("ZDJECIE", "Bill picture  " + bill.getPicturePath());
			   
		
               Display display = getWindowManager().getDefaultDisplay();
               Point size = new Point();
               display.getSize(size);
           
               
			   Bitmap bitmap = BitmapFactory.decodeFile(bill.getPicturePath());
               bitmap = Bitmap.createScaledBitmap(bitmap,size.x, size.x, true);
			
               
               
               Matrix matrix = new Matrix();
               matrix.postRotate(90);
               Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
               
               
			   currentPicture.setImageBitmap(rotatedBitmap);
			   
		   }
		   else {
			   currentBillPictureTitle.setVisibility(TableLayout.GONE);
			   currentBillPictureTitle.invalidate();
			   
		   }
	    }		
	}
	
	public void removeBill() {
		if (bill != null){			
			GlobalParams.getInstance().billdatasource.deleteBill(bill);			
			List<Unit> units = GlobalParams.getInstance().unitdatasource.getAllUnitForBill(bill.getId());	
			for (Unit st : units) {	
				GlobalParams.getInstance().unitdatasource.deleteUnit(st);				
			}			
		}		
	}	
	
	public void createTableUnitResult(Integer billId) {
		
		 	List<Unit> units = GlobalParams.getInstance().unitdatasource.getAllUnitForBill(bill.getId());		    	
		 	currentBillUnits.removeAllViews();
		 	for (Unit st : units) {	 		
		 		
		 		TableRow row = new TableRow(this);
			    TextView unitName = new TextView(this);
			    TextView unitPrice = new TextView(this);
			    TextView unitMetric = new TextView(this);
			    TextView unitValue = new TextView(this);
			    
			    unitName.setText(st.getName());
			    unitName.setBackgroundResource(R.drawable.cell_shape);
			    
			    unitPrice.setText(st.getPrice().toString());
			    unitPrice.setBackgroundResource(R.drawable.cell_shape);
			    
			    unitMetric.setText(st.getMetric());
			    unitMetric.setBackgroundResource(R.drawable.cell_shape);
			   
			    unitValue.setText(st.getValue().toString());
			    unitValue.setBackgroundResource(R.drawable.cell_shape);
			   
			    row.addView(unitName);
			    row.addView(unitPrice);
			    row.addView(unitMetric);
			    row.addView(unitValue);
			    
			    currentBillUnits.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				
		 	}

	    }

	

}
